package com.iemes.controller.device;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.base.ConstantController;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DevOperationFormMap;
import com.iemes.entity.DeviceCheckFormMap;
import com.iemes.mapper.DeviceMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//设备检验
@Controller
@RequestMapping("/devicecheck/")
public class DeviceCheckController extends BaseController {

    @Inject
    private UserMapper userMapper;

    @Inject
    private DeviceMapper deviceMapper;

    @RequestMapping("devicecheck")
    public String listUI(Model model) throws Exception {
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>","<button type=\"button\" id=\"kan\" class=\"btn btn-danger marR10\">查看操作</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/device/check/devicecheck_list";
    }

    @ResponseBody
    @RequestMapping("check_findByPage")
    public PageInfo findByPage(String pageNow,
                               String pageSize, HttpServletRequest request) throws Exception {

        //采用pageinfo分页
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }

        DeviceCheckFormMap deviceCheckFormMap = getFormMap(DeviceCheckFormMap.class);
        deviceCheckFormMap=toFormMap(deviceCheckFormMap, pageNow, pageSize,deviceCheckFormMap.getStr("orderby"));
        String name=request.getParameter("deviceCheckEmp");
        if(name==null || name.equals("")){
            List<DeviceCheckFormMap> deviceCheckFormMaps = deviceMapper.getDeviceCheck();
            PageInfo pageInfo = new PageInfo<>(deviceCheckFormMaps);
            return pageInfo;
        }else {
            List<DeviceCheckFormMap> deviceCheckFormMaps = deviceMapper.getDeviceCheckByEmp(name);
            PageInfo pageInfo = new PageInfo<>(deviceCheckFormMaps);
            return pageInfo;
        }

    }


    @RequestMapping("check_addUI")
    public String addUI(Model model) throws Exception{
        model.addAttribute("list",deviceMapper.getDeviceName());
        //获取所有用户
        model.addAttribute("emp", getEmp());
        model.addAttribute("devType", deviceMapper.getDeviceType());
        model.addAttribute("id",deviceMapper.getDeviceCheck().size()+1);
        return Common.BACKGROUND_PATH +"/device/check/devicecheck_add";
    }

    @RequestMapping("check_editUI")
    public String editUI(Model model) throws Exception{
        String id=getPara("id");
        if(Common.isNotEmpty(id)){
            //获取主要数据
            model.addAttribute("user",userMapper.findbyFrist("deviceCheckId", id, DeviceCheckFormMap.class));
            //获取关联数据
            model.addAttribute("bom", deviceMapper.getdo(id));
            model.addAttribute("emp", getEmp());
            model.addAttribute("devType", deviceMapper.getDeviceType());

        }
        return Common.BACKGROUND_PATH+"/device/check/devicecheck_edit";
    }

    //返回页面并且
    @ResponseBody
    @RequestMapping("dp")
    public PageView getDet(){
        pageView.setRecords(deviceMapper.getdo(getPara("id")));
        return pageView;
    }

    @ResponseBody
    @RequestMapping("check_addEntity")
    public String addEntity(Model model, HttpServletRequest request) throws Exception {
       DeviceCheckFormMap deviceCheckFormMap = getFormMap(DeviceCheckFormMap.class);
        Date date= new Date();
        SimpleDateFormat dateFormat_min=new SimpleDateFormat("YYYY-MM-dd");
        deviceCheckFormMap.set("deviceCheckDate",dateFormat_min.format(date));

        try {
            //获取新添加数据

            String[] itemNoArr = request.getParameterValues("ly_dev_part");
            String[] itemUseNumberArr = request.getParameterValues("ly_dev_describe");
            //DevOperationFormMap
            userMapper.deleteByAttribute("deviceCheckId", deviceCheckFormMap.get("deviceCheckId")+"", DeviceCheckFormMap.class);
            if(null != itemNoArr ){
                for(int i = 0 ;i<itemNoArr.length;i++){
                    DevOperationFormMap itemBomFormMap = new DevOperationFormMap();
                    itemBomFormMap.put("ly_dev_part", itemNoArr[i]);
                    itemBomFormMap.put("ly_dev_describe", itemUseNumberArr[i]);
                    itemBomFormMap.put("ly_dev_keyid", deviceCheckFormMap.get("deviceCheckId"));
                    baseMapper.addEntity(itemBomFormMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseMapper.addEntity(deviceCheckFormMap);
        JSONObject json = new JSONObject();
        json.put("result", ConstantController.SUCCESS);
        return json.toString();
    }

    //设备检验所需要的步骤
    @RequestMapping("kan")
    public String openKan(Model model){
        //获取所走的步骤
        String id=getPara("id");
        if(Common.isNotEmpty(id)){
            //获取主要数据
            model.addAttribute("user",userMapper.findbyFrist("deviceCheckId", id, DeviceCheckFormMap.class));
            //获取关联数据
            model.addAttribute("bom", deviceMapper.getdo(id));
            model.addAttribute("emp", getEmp());
            model.addAttribute("devType", deviceMapper.getDeviceType());
        }
        return Common.BACKGROUND_PATH+"/device/check/devicecheck_kante";
    }



    @ResponseBody
    @RequestMapping("check_editEntity")
    //	@Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备例检管理",methods="例检信息编辑")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity(HttpServletRequest request) throws Exception{
        DeviceCheckFormMap deviceCheckFormMap=getFormMap(DeviceCheckFormMap.class);
        //删除数据
        Date date= new Date();
        SimpleDateFormat dateFormat_min=new SimpleDateFormat("YYYY-MM-dd");
        deviceCheckFormMap.set("deviceCheckDate",dateFormat_min.format(date));
        try {
            //获取新添加数据
            String[] itemNoArr = request.getParameterValues("ly_dev_part");
            String[] itemUseNumberArr = request.getParameterValues("ly_dev_describe");
            //DevOperationFormMap
            userMapper.deleteByAttribute("ly_dev_keyid", deviceCheckFormMap.get("deviceCheckId")+"", DevOperationFormMap.class);
            if(null != itemNoArr ){
                for(int i = 0 ;i<itemNoArr.length;i++){
                    DevOperationFormMap itemBomFormMap = new DevOperationFormMap();
                    itemBomFormMap.put("ly_dev_part", itemNoArr[i]);
                    itemBomFormMap.put("ly_dev_describe", itemUseNumberArr[i]);
                    itemBomFormMap.put("ly_dev_keyid", deviceCheckFormMap.get("deviceCheckId"));
                    baseMapper.addEntity(itemBomFormMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseMapper.deleteByAttribute("deviceCheckId",deviceCheckFormMap.get("deviceCheckId")+"", DeviceCheckFormMap.class);
        baseMapper.addEntity(deviceCheckFormMap);
        JSONObject json = new JSONObject();
        json.put("result", ConstantController.SUCCESS);
        return json.toString();
    }


    //删除
    @ResponseBody
    @RequestMapping("deleteCheck")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备例检管理",methods="例检信息删除")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delCheck() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            userMapper.deleteByAttribute("deviceCheckId", id, DeviceCheckFormMap.class);
        }
        return "success";
    }
}
